package me.m1key.springtx.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.transaction.annotation.Transactional;

import me.m1key.springtx.entities.Dog;

public class DogsDaoImpl implements DogsDao {

	@PersistenceContext
	private EntityManager em;

	@SuppressWarnings("unchecked")
	@Transactional(readOnly = true)
	public List<Dog> retrieveAllDogs() {
		return em.createQuery("from Dog").getResultList();
	}

	@Transactional(readOnly = true)
	public Long retrieveNumberOfDogs() {
		System.out.println(em);
		return (Long) em.createQuery("select count(d) from Dog d")
				.getSingleResult();
	}

	@Transactional(readOnly = false)
	public void persistDog(Dog dog) {
		em.persist(dog);
	}

}
